package rmhc
{
	import flash.display.MovieClip;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.system.Security;
	
	import rmhc.api.ApiWrapper;
	import rmhc.api.fb.FbApi;
	import rmhc.api.vk.VkApi;
	import rmhc.gui.AvatarScreen;
	import rmhc.gui.PostScreen;
	import rmhc.gui.ScreenManager;
	
	[SWF(width='807', height='593', backgroundColor='#FFFFFF', frameRate='30')]
	public class RMHCApp extends MovieClip
	{
		protected var scr:ScreenManager;
		protected var api:ApiWrapper;
		
		protected static var inst:RMHCApp;
		
		public function RMHCApp()
		{
			super();
			inst = this;
			addEventListener( Event.ADDED_TO_STAGE , init );
		}
		
		protected function init( e:Event ):void
		{
			//stage.scaleMode = StageScaleMode.NO_SCALE;
			//stage.align = StageAlign.TOP;
			//Security.allowDomain( "*" );
			trace( "server url" , getServerUrl() );
			scr = new ScreenManager();
			addChild( scr );
			
			var params:Object = stage.loaderInfo.parameters;
			api = ApiWrapper.getApi( params );		
			api.addEventListener( Event.CONNECT , construct );
			api.connect();
			//construct();
		}
		protected function construct( e:Event = null ):void
		{
			showGreetScreen();
		}
		
		//МЕТОДЫ ЭКРАНОВ
		public function showGreetScreen():void
		{
			scr.showGreet();
		}
		public function showAvatarScreen():void
		{
			var s:AvatarScreen = scr.showAvatar();
			s.addEventListener( Event.COMPLETE , onAvatarComplete );
		}
		protected function onAvatarComplete( e:Event ):void
		{
			trace( "onAvatarComplete" );
			showGreetScreen();
		}
		public function showPostScreen():void
		{
			var s:PostScreen = scr.showPost();
			s.addEventListener( Event.COMPLETE , onPostComplete );
		}
		protected function onPostComplete( e:Event ):void
		{
			trace( "onPostComplete" );
			showGreetScreen();
		}
		
		public static function getApp():RMHCApp
		{
			return inst;
		}
		public static function getApi():ApiWrapper
		{
			return inst.api;
		}
		
		public static function getServerUrl():String
		{
			var u:String = ( inst.parent as IAppPreloader ).getServerUrl();
			return u;
			//return "http://mind2create.ru/vk/rmhc/";
			//return "http://rmhc/flash/";
		}
		public static function getPhpUrl():String
		{
			return ( inst.parent as IAppPreloader ).getPhpUrl();
		}
	}
}